/**
 *
 */
package cn.rocksolid.sport.common.utils;

import java.awt.image.BufferedImage;
import java.util.Properties;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

/**
 * @author axl
 *
 */
public class CaptchaUtils {

  public static final String DEFAULT_BORDER_STYLE = "no";
  public static final String DEFAULT_FONT_COLOR = "black";
  public static final String DEFAULT_CHAR_LEN = "4";
  public static final String DEFAULT_CHAR_SPACE = "4";
  public static final String NUMERIC_STRING = "0123456789";

  private CaptchaUtils() {
  }

  private static final DefaultKaptcha CAPTCHA_PRODUCER;
  private static final DefaultKaptcha CAPTCHA_NUMERIC_PRODUCER;

  static {
    Properties properties = new Properties();
    properties.put(Constants.KAPTCHA_BORDER, DEFAULT_BORDER_STYLE);
    properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, DEFAULT_FONT_COLOR);
    properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, DEFAULT_CHAR_LEN);
    properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, DEFAULT_CHAR_SPACE);

    CAPTCHA_PRODUCER = new DefaultKaptcha();
    CAPTCHA_PRODUCER.setConfig(new Config(properties));

    Properties numericProperties = new Properties();
    numericProperties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, NUMERIC_STRING);
    numericProperties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, DEFAULT_CHAR_LEN);
    numericProperties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, DEFAULT_CHAR_SPACE);
    CAPTCHA_NUMERIC_PRODUCER = new DefaultKaptcha();
    CAPTCHA_NUMERIC_PRODUCER.setConfig(new Config(numericProperties));
  }

  public static String createText() {
    return CAPTCHA_PRODUCER.createText();
  }

  public static String createNumericText() {
    return CAPTCHA_NUMERIC_PRODUCER.createText();
  }

  public static BufferedImage createImage(String text) {
    return CAPTCHA_PRODUCER.createImage(text);
  }
}
